﻿<?
class TypesController extends AppController
{
    var $name='Types';
    var $components=array('Auth');
    var $helpers=array('Treetb','Form','Html','Javascript');
    
    function beforeFilter()
    {
        $this->layout="admin";
        $this->userScope = array('admin_leve'=>1);
        $this->authorize = 'controller';
        $this->Auth->loginError = '未能登录成功,密码或帐号错误';
        $this->Auth->authError='还未登录或者你没有管理员权限';
	
    }
    
    function isAuthorized()
    {
           if($this->Auth->user('admin_leve')>0)
            return true;
        else
            return false;
    }

      function admin_index()
    {
        $treeData=$this->Type->findAllThreaded ();
        $this->set('treeArray',$treeData);
        $typeTree = $this->Type->generatetreelist(null,null,null,'---');
        $this->set('tt',$typeTree);
        $this->pageTitle='分类管理';
    }
    
    function admin_remove($id=null)
    {
        if(empty($id) or !is_numeric($id)){
            $this->flash('请选择要删除的分类','index',5);
            return;
        }
        if($this->Type->del($id)){
            $this->flash('删除成功','index',5);
            return;
        }else{
            $this->flash('出现未知错误,未能删除成功','index',5);
            return;
        }
    }
      
    
    function admin_add($pid=0)
    {
       if(!empty($this->data) ){
          $this->Type->create();
           if($this->Type->save($this->data)){
                $this->flash('添加分类成功','index',5);
                return;
            }else{
                if(!empty($this->data['Type']['parent_id']))
                        $pid=$this->data['Type']['parent_id'];
            }            
        }
        if($pid !=0 ){
             if(!($cat=$this->Type->findById($pid))){
                $this->flash('要添加到的目标分类并不存在'.$pid,'index',5);
                return;
            }
            $pnode=$cat['Type']['name'];
             $this->set('typename',$pnode);  
         }   
         $this->set('pid',$pid); 
         $this->pageTitle='分类管理:添加分类';
    }
    
    function admin_moveto($id,$pid=0)
    {
        if(empty($id) or !is_numeric($id) or !is_numeric($pid) or $id==$pid){
            $this->flash('请选择要移动的分类和填写正确的目标分类','index',5);
            return;
        }
        
        if($pid !=0 and !$this->Type->findById($pid)){
            $this->flash('要移动到的目标分类并不存在','index',5);
            return;
        }
        $this->id=$id;
        if($this->Type->save(array('parent_id' => $pid )))
            $this->flash('转移成功','index',5);
    }
    
    function admin_edit($id=0)
    {
        if(!empty($this->data)){
            if($this->Type->save($this->data)){
                $this->flash('修改成功','index',5);
                return;
            }
        }else{
            if($id==0 or !($this->data=$this->Type->findById($id))){
                $this->flash('请选择要修改的分类信息','index',5);
                return;
            }
        }
        $this->pageTitle='分类管理:编辑分类';
    }
    
     //方法，生成一个分类(栏目)页的静态页
    function admin_createTypeHtml($tid)
    {
        $outHtml=$this->requestAction("/html/typePage/$tid");
        if(strpos($outHtml,'error:')===0){
            $errstr=str_replace('error:','',$outHtml);
            $this->flash($errstr,array('controller'=>'Type','action'=>'index','prefix'=>'admin'),5);
            return;
        }
        $this->Type->id=$tid;
        $filepath=$this->Type->field('htmlPath');
        $filepath='../../html/'.$filepath.'.html';
        if(file_put_contents($filepath,$outHtml)){
            $this->flash('文件成功生成','-1',5);
            return;
        }else{
            $this->flash('文件生成失败','-1',5);
            return;
        }
    }
    
     //方法，生成全部静态页面
    function admin_createAllHtml()
    {
        $tids=$this->Type->find('all',array('conditions' =>array('hide'=>0),'fields'=>array('id','htmlPath')));
        foreach($tids as $t){
            $outHtml=$this->requestAction("/html/TypePage/{$t['Type']['id']}");
            if(strpos($outHtml,'error:')===0){
                $errstr=str_replace('error:','',$outHtml);
                $this->flash($errstr,'-1',5);
                return;
            }
            $filepath=$t['Type']['htmlPath'];
            $filepath='..'.DS.'../html/'.$filepath.'.html';
            if(!file_put_contents($filepath,$outHtml)){
                $this->flash('文件生成出现错误','-1',5);
                return;
            }
        }
        $this->flash('成功生成完所有分类页面的静态页面','-1',5);        
    }
}
?>